using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections.Specialized;

namespace Speedball2 {
  public class Input : ICloneable {
    private BitVector32 _flags;

    private int _bit0_Up;
    private int _bit1_Down;
    private int _bit2_Left;
    private int _bit3_Right;
    private int _bit4;
    private int _bit5;
    private int _bit6;
    private int _bit7_Button;

    public Input () {
      _bit0_Up = BitVector32.CreateMask ();
      _bit1_Down = BitVector32.CreateMask (_bit0_Up);
      _bit2_Left = BitVector32.CreateMask (_bit1_Down);
      _bit3_Right = BitVector32.CreateMask (_bit2_Left);
      _bit4 = BitVector32.CreateMask (_bit3_Right);
      _bit5 = BitVector32.CreateMask (_bit4);
      _bit6 = BitVector32.CreateMask (_bit5);
      _bit7_Button = BitVector32.CreateMask (_bit6);
    }

    public Input (int flags)
      : this () {
      Init (flags);
    }

    public void Reset () {
      Init (0);
    }

    private void Init (int flags) {
      Bit0_IsUp = ((flags >> 0) & 1) == 1;
      Bit1_IsDown = ((flags >> 1) & 1) == 1;
      Bit2_IsLeft = ((flags >> 2) & 1) == 1;
      Bit3_IsRight = ((flags >> 3) & 1) == 1;
      Bit7_IsButton = ((flags >> 7) & 1) == 1;
    }

    public bool Bit0_IsUp {
      get {
        return (_flags [_bit0_Up]);
      }
      set {
        _flags [_bit0_Up] = value;
      }
    }

    public bool Bit1_IsDown {
      get {
        return (_flags [_bit1_Down]);
      }
      set {
        _flags [_bit1_Down] = value;
      }
    }

    public bool Bit2_IsLeft {
      get {
        return (_flags [_bit2_Left]);
      }
      set {
        _flags [_bit2_Left] = value;
      }
    }

    public bool Bit3_IsRight {
      get {
        return (_flags [_bit3_Right]);
      }
      set {
        _flags [_bit3_Right] = value;
      }
    }

    public bool Bit7_IsButton {
      get {
        return (_flags [_bit7_Button]);
      }
      set {
        _flags [_bit7_Button] = value;
      }
    }

    public bool HasDirection {
      get {
        return (Bit0_IsUp || Bit1_IsDown || Bit2_IsLeft || Bit3_IsRight);
      }
    }

    public bool HasNoDirection {
      get {
        return (!Bit0_IsUp && !Bit1_IsDown && !Bit2_IsLeft && !Bit3_IsRight);
      }
    }

    //public int DirectionData {
    //  get {
    //    return (_flags.Data & 15);
    //  }
    //  set {
    //  }
    //}

    public DirBits DirBits {
      get {
        return (new DirBits (_flags.Data & 15));
      }
      //set {
      //}
    }

    public int Data {
      get {
        return (_flags.Data);
      }
    }

    public object Clone () {
      Input input = new Input ();
      input._flags = _flags;
      input._bit0_Up = _bit0_Up;
      input._bit1_Down = _bit1_Down;
      input._bit2_Left = _bit2_Left;
      input._bit3_Right = _bit3_Right;
      input._bit4 = _bit4;
      input._bit5 = _bit5;
      input._bit6 = _bit6;
      input._bit7_Button = _bit7_Button;
      return (input);
    }
  }
}
